package demo;
import com.tedu.InputUtil;

import java.sql.SQLOutput;
import java.util.List;

/**
 * 用户注册：顺序输入用户名，密码，昵称，年龄;
 */
public class Demo {
    public static void main(String[] args) {
        //用户注册
//        regUser();
//        loginUser();
        findAll();

    }

    public static void findAll(){
        UserMapper userMapper = new UserMapper();
        List<User> users = userMapper.selectAll();
        for (User user : users){
            System.out.println(user);
        }
    }


    /**
     * 修改用户信息操作
     */

    public static void updateUser(){
        User user = InputUtil.getInputObject(new User(),"修改信息");
        UserMapper userMapper = new UserMapper();
        if (userMapper.selectByUsername(user.getUsername())==null){
            System.out.println("该用户不存在");
        }else{
            int num = userMapper.update(user);
            if (num>0){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        }
    }

    /**
     * 用户登录操作
     */
    public static void loginUser(){
        /*
        如果用户名输入错误，提示用户名错误；如果密码错误，提示密码错误，
        如果都正确提示登录成功
         */
        UserLoginDTO dto = InputUtil.getInputObject(new UserLoginDTO(),"欢迎登录");
        System.out.println(dto);
        User user = new UserMapper().selectByUsername(dto.getUsername());
        System.out.println(user);
        if(user!=null) {
            if (user.getPassword().equals(dto.getPassword())){
                System.out.println("登陆成功");
            }else {
                System.out.println("密码错误");
            }
        }else{
            System.out.println("用户名输入有误");
        }
    }

    /**
     * 用户注册操作
     */
    public static void regUser(){
        User user = InputUtil.getInputObject(new User(),"欢迎注册");
        System.out.println(user);
        UserMapper userMapper = new UserMapper();
        if (userMapper.selectByUsername(user.getUsername())==null){
            int num = userMapper.insert(user);
            System.out.println(num>0?"注册成功":"注册失败");
        }else {
            System.out.println("该用户已存在");
        }

    }



}
